library(foreign)
library(ggplot2)
library(grid)
library(gridExtra)
library(scales)

rm(list=ls())

setwd("Yourpath")$
  
source("fine_grid.R") # ggplot layers
source("summarySE.R") # summary stats (for figure 5)

#################################################
#                                               #
#      PLOT AGE RESULTS                         #
#                                               #
#################################################


# (i) Treatment and post-treatment effect for districts in VD

treatment_effect <- matrix(NA,19,1)
posttreatment_effect <- matrix(NA,19,1)


for (i in 2201:2219){
Y_0 = read.csv(paste("Y_synthetic_district_",i,".txt",sep=""), sep = "\t")[,2]
Y_1 = read.csv(paste("Y_treated_district_",i,".txt",sep=""), sep = "\t")[,2]
treatment_effect[(i-2200)]=mean(Y_1[33:65]-Y_0[33:65])
posttreatment_effect[(i-2200)]=mean(Y_1[66:125]-Y_0[66:125])
}

# (ii) Merge district results with age data

df<- read.dta("data7.dta",convert.factors = F) # convert 

data <- data.frame(bnum=df$bnum,
                   district=df$bnam,
                  treatment_effect=treatment_effect,
                  posttreatment_effect=posttreatment_effect
      )


df2<- read.dta("data8.dta",convert.factors = F) # convert 

data_all <- merge(data, df2, by=c("district"))

data_all$posttreatment_effect_rescaled <- data_all$posttreatment_effect-mean(data_all$posttreatment_effect)


data_all_new <- melt(data_all, id.vars = c("bnum","district","cvoting_fisttime_sum_share_hi","cvoting_under30_sum_share_hi","no_years_exp_cvoting_share_hi"))
data_all_new <- data_all_new[data_all_new$variable=="posttreatment_effect_rescaled",]

data_all_new$group <- data_all_new$variable
data_all_new$posttreatment_effect_rescaled<- data_all_new$value

drops <- c("variable","value")
data_all_new <- data_all_new[ , !(names(data_all_new) %in% drops)]


df <- melt(data_all_new, id.vars = c("bnum","district","posttreatment_effect_rescaled","group"))
df$variable_num <- as.numeric(df$variable)

df2 <- summarySE(df, measurevar="posttreatment_effect_rescaled", groupvars=c("variable_num","value"))
df2$description <- matrix(NA,length(df2$value),1)

df$description[df$value==0] <- "Low"
df$description[df$value==1] <- "High"

# (iii) Figure

d <- ggplot(df,aes(y=posttreatment_effect_rescaled,x=variable_num,fill=value,group=value))
d +   #geom_point(position=position_jitterdodge(dodge.width=0.7),alpha=0.5) +  
 geom_point(data=df2,aes(x=variable_num, ymin=posttreatment_effect_rescaled),position=position_dodge(width=0.3),size=3.1,width=0.5) +
  geom_errorbar(data=df2, aes(x=variable_num, ymin=posttreatment_effect_rescaled-1.96*se, ymax=posttreatment_effect_rescaled+1.96*se),position=position_dodge(width=0.3),width=0.5) + 
  theme_bw_finegrid(base_size = 31) +
  xlab("") + ylab("Long-Term Turnout Effect (Percentage Points) \n") +
  scale_x_continuous(breaks=c(1:3),labels=c("First-time \n voters \n (share)  ","Voters \n under 30  \n (share)  ","Compulsory voting \n experience \n (share)  ")) +
  scale_y_continuous() +
  guides(fill=FALSE)+ 
  geom_vline(xintercept=1.5,linetype="dashed") +
  geom_vline(xintercept=2.5,linetype="dashed") +
  geom_text(aes(label=description,y=-0.075), position=position_dodge(width=0.9),size=7.5)


ggsave(file="Figure3.pdf",width=15,height=10 )

